package com.ai.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ai.web.service.ProductInfoService;
import com.ai.web.vo.ProductInfoV;
import com.ai.web.vo.QueryData;
import com.ai.web.vo.ResultModel;
import com.ai.web.vo.UploadFileV;

/**
 * 产品介绍
* @ClassName: ProductInfoController 
* @Description: TODO 
* @author:  
* @date: 2014年3月24日
*
 */
@Controller
@RequestMapping("/productInfo")
public class ProductInfoController extends BaseController {
    

    private static Logger logger = Logger.getLogger(ProductInfoController.class);
    
    @Autowired
    private ProductInfoService productInfoService;
    
    @RequestMapping(value = "/list")
    public String list() {
        
        logger.info("hello......test");
        return "productInfo/productInfo_list";
    }
    
    @RequestMapping(value = "/query", produces = "application/json")
    @ResponseBody
    public QueryData query(@RequestParam HashMap<String, Object> paramMap)
            throws Exception {
        
        Map<String, Object> conditionMap = buildCondition(paramMap);
        logger.info("---- " + conditionMap);
        
        QueryData qd = productInfoService.queryListAndCount(conditionMap);
        
        return qd;
    }
    
    @RequestMapping(value = "/new")
    public String newproductInfo(Model model) {
        
        ProductInfoV productInfoV = new ProductInfoV();
        model.addAttribute(productInfoV);
        
        return "productInfo/productInfo_template";
    }
    
    @RequestMapping(value = "/update/{id}")
    public String updateproductInfo(@PathVariable("id") int id, Model model)
            throws Exception {
        
        ProductInfoV productInfoV = productInfoService.getProductInfo(id);
        logger.info("----- getUser:" + productInfoV);
        
        if (productInfoV == null) {
            // exception redirect error page
            // TODO
        }
        model.addAttribute(productInfoV);
        
        return "productInfo/productInfo_template";
    }
    
    @RequestMapping(value = "/save", produces = "application/json")
    @ResponseBody
    public ResultModel saveproductInfo(ProductInfoV productInfoV) throws Exception {
        
        logger.info("-----save:" + productInfoV);
        
        ResultModel rm = productInfoService.saveProductInfo(productInfoV);
        
        return rm;
    }
    
    @RequestMapping(value = "/delete", produces = "application/json")
    @ResponseBody
    public ResultModel deleteproductInfo(@RequestParam String delIds)
            throws Exception {
        
        System.out.println(delIds);
        
        List<Integer> userIds = getDeleteIds(delIds);
        ResultModel rm = productInfoService.deleteProductInfos(userIds);
        
        return rm;
    }
    
    //文件上传
    @RequestMapping(value = "/upload", produces = "application/json")
    @ResponseBody
    public ResultModel upload(HttpServletRequest request,HttpServletResponse response) throws Exception{
        
        //返回文件名称、文件路径
        List<UploadFileV> UploadFileVList = productInfoService.uploadFiles(request);
        
        ResultModel rm = new ResultModel();
        rm.setSuccessCode();
        rm.setMsg("上传成功！");
        return rm;
    }
    
}
